using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using Entities;

namespace DAL
{
    public class tbHangSanXuatDAL
    {
         private SqlConnection cn = null;
        public tbHangSanXuatDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);           
            cn.Open();
        }
        public void Insert(tbHangSanXuat HangSanXuat)
        {
            SqlCommand cm = new SqlCommand("HangSanXuat_Insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenHangSanXuat ", HangSanXuat.TenHangSanXuat);
            cm.Parameters.AddWithValue("@DiaChi", HangSanXuat.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", HangSanXuat.DienThoai);
            cm.Parameters.AddWithValue("@Email", HangSanXuat.Email);          

            cm.ExecuteNonQuery();

        }
        public void Update(tbHangSanXuat HangSanXuat)
        {
            SqlCommand cm = new SqlCommand("HangSanXuat_Update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IDHangSanXuat ", HangSanXuat.IDHangSanXuat);
            cm.Parameters.AddWithValue("@TenHangSanXuat", HangSanXuat.TenHangSanXuat);
            cm.Parameters.AddWithValue("@DiaChi", HangSanXuat.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", HangSanXuat.DienThoai);
            cm.Parameters.AddWithValue("@Email", HangSanXuat.Email);          

            cm.ExecuteNonQuery();
        }
        public void Delete(int Id)
        {
            SqlCommand cm = new SqlCommand("HangSanXuat_Delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IDHangSanXuat ", Id);
            cm.ExecuteNonQuery();
        }
        public Entities.tbHangSanXuat.tbHangSanXuatCollection Get(string Where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("HangSanXuat_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", System.Data.DbType.Int32)).Direction = System.Data.ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbHangSanXuat.tbHangSanXuatCollection HangSanXuatcollection = new tbHangSanXuat.tbHangSanXuatCollection();
            while (dr.Read())
            {
                tbHangSanXuat HangSanXuat = new tbHangSanXuat();
                HangSanXuat.IDHangSanXuat =int.Parse (dr.GetValue(0).ToString());
                HangSanXuat.TenHangSanXuat = dr.GetValue(1).ToString();
                HangSanXuat.DiaChi = dr.GetValue(2).ToString();
                HangSanXuat.DienThoai = int.Parse(dr.GetValue(3).ToString());
                HangSanXuat.Email = dr.GetValue(4).ToString();

                HangSanXuatcollection.Add(HangSanXuat);            
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return HangSanXuatcollection;
        }

    }
}
